<template>
  <div class="container">
    <el-card
      style="margin: 5px 5px 0 5px"
    >
      <!-- 放置一个面包屑 -->
      <el-breadcrumb v-show="subjectID" separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{ subjects.subjectName }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        <el-row style="margin-top: 10px">
          <el-divider></el-divider>
        </el-row>
      </el-breadcrumb>
      <!-- 放置一个表单 -->
      <el-form
        :inline="true"
        label-width="80px"
        :model="formInline"
        class="demo-form-inline"
        :style="{ clock: '#909399' }"
      >
        <el-form-item label="目录名称" style="height: 28px">
          <el-input v-model="formInline.text"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model.trim="formInline.region" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="deleteData">清除</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-col>
            <el-button
              @click="fn"
              v-show="subjectID"
              type="text"
              icon="el-icon-back"
              >返回学科</el-button
            >
            <el-button @click="modifyAdd" type="success" icon="el-icon-edit"
              >新增目录</el-button
            >
          </el-col>
        </el-form-item>
      </el-form>
      <el-alert
        :title="`数据一共${page.counts}条`"
        type="info"
        show-icon
        :closable="false"
        :style="{ margin: '0 0 15px 0' }"
      />
      <el-table
        :data="list"
        style="width: 100%"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      >
        <el-table-column fixed type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template v-slot="{ row }">
            <span>{{ row.addDate | parseTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="{ row }">
            {{ row.state === 1 ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="{ row }">
            <el-button @click="handleClick(row)" type="text" size="medium">{{
              row.state === 1 ? "启用" : "禁用"
            }}</el-button>
            <el-button
              @click="modifyAdd(row)"
              :disabled="row.state === 0"
              type="text"
              size="medium"
              >修改</el-button
            >
            <el-button
              @click="removeDirectory(row)"
              :disabled="row.state === 0"
              type="text"
              size="medium"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row
        type="flex"
        justify="end"
        align="middle"
        style="height: 28px; margin-top: 20px"
      >
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageNumber"
          :page-size="page.pagesize"
          :current-page.sync="page.page"
          layout="prev, pager, next,sizes,jumper "
          :total="page.counts"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <DirectorysAdd
      ref="updateAdd"
      :isShow.sync="isShow"
      @toUpdateList="getList"
      :subjectsID="subjectID"
    ></DirectorysAdd>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { changeState, remove } from '../../api/hmmm/directorys'
import DirectorysAdd from '../components/directorys-add.vue'
import { detail } from '../../api/hmmm/subjects'
export default {
  data () {
    return {
      formInline: {
        text: '',
        region: ''
      },
      isShow: false, // 新增编辑的弹层
      subjects: {}, // 学科详情
      subjectID: this.$route.params.id, // 学科页面传过来的学科的id
      pageNumber: [5, 10, 20, 40, 60, 80, 100],
      list: [], // 目录数据
      page: {
        page: 1, // 当前页码
        pagesize: 5, // 每页多少条
        counts: 0,
        directoryName: null, // 目录名称
        state: null, // 目录状态
        id: null, // 学科的id
        subjectID: null // 目录列表需要的参数
      }
      // loading: false
    }
  },
  created () {
    this.getList()
  },
  components: {
    DirectorysAdd
  },
  methods: {
    // 自定义事件
    ...mapActions(['getDirectoryList']),
    // 封装获取列表数据
    async getList () {
      // this.loading = true
      // 调用方法
      // 判断输入框内是否有内容
      if (this.formInline.region.length) {
        this.page.state = this.formInline.region - 0
      }
      if (this.subjectID) {
        this.page.subjectID = this.subjectID
        this.page.id = this.subjectID
        const { data } = await detail({ id: this.page.id })
        this.subjects = data
        console.log(this.subjects)
      }
      this.page.directoryName = this.formInline.text
      const { data } = await this.getDirectoryList(this.page)
      this.list = data.items
      this.page.counts = data.counts
      // this.loading = false
    },
    // 查询
    async onSubmit () {
      this.page.page = 1
      this.getList()
    },
    // 清除
    deleteData () {
      // 重置表单内容
      this.formInline = {
        text: '',
        region: ''
      }
      this.page.page = 1
      this.getList()
    },
    // 点击切换分页
    handleSizeChange (val) {
      // 将当前点击的分页数赋值给分页
      this.page.pagesize = val
      // 更新视图
      this.getList()
    },
    // 点击切换页
    handleCurrentChange (val) {
      // 将当前点击的数字赋值给页码
      this.page.page = val
      // 更新视图
      this.getList()
    },
    // 禁用和启用
    async handleClick (row) {
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      // 发起请求，更新后台数据
      await changeState(row)
      this.$message.success('操作成功')
    },
    // 删除
    async removeDirectory (row) {
      try {
        // 询问用户是否删除
        await this.$confirm('您确定删除吗？')
        // 调用接口，删除数据
        await remove(row)
        // 删除成功后提示用户
        this.$message.success('删除成功')
        // 重新拉取数据
        this.getList()
      } catch (err) {
        console.log(err)
      }
    },
    // 修改   &&   新增
    async modifyAdd (row) {
      //  调用子组件的方法
      await this.$refs.updateAdd.updateDirectory(row, this.subjectID)
      // 显示弹层
      this.isShow = true
    },
    fn () {
      this.$router.push('/subjects/list')
    }
  }
}
</script>

<style >
</style>
